/*
* Copyright (c) 2008, Vladimir Gumenuk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the copyright holder nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "RealGNX4Lib.h"
#include "RealGNX4LibDoc.h"
#include "MainFrm.h"
#include "BankNamesDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_WM_CREATE()
    ON_COMMAND(ID_VIEW_BANKNAMES, &CMainFrame::OnViewBanknames)
    ON_UPDATE_COMMAND_UI(ID_VIEW_BANKNAMES, &CMainFrame::OnUpdateViewBanknames)
    ON_COMMAND(ID_MOVE_UP_BTN, &CMainFrame::OnMovePresetUp)
    ON_UPDATE_COMMAND_UI(ID_MOVE_UP_BTN, &CMainFrame::OnUpdateMovePresetUp)
    ON_COMMAND(ID_MOVE_DOWN_BTN, &CMainFrame::OnMovePresetDown)
    ON_UPDATE_COMMAND_UI(ID_MOVE_DOWN_BTN, &CMainFrame::OnUpdateMovePresetDown)
    ON_COMMAND(ID_PRESET_LOADFROMG4PFILE32777, &CMainFrame::OnLoadFromG4PFile)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if( !m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC ) ||
		!m_wndToolBar.LoadToolBar( IDR_MAINFRAME ) )
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if( !m_wndStatusBar.Create( this ) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT) ) )
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	m_wndToolBar.EnableDocking( CBRS_ALIGN_ANY );
	EnableDocking( CBRS_ALIGN_ANY );
	DockControlBar( &m_wndToolBar );

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers




void CMainFrame::OnViewBanknames()
{
    CRealGNX4LibDoc *pDoc = GetDoc();
    if( pDoc )
    {
        CBankNamesDlg dlg( pDoc->GetTBLHeader() );
        if( IDOK == dlg.DoModal() && dlg.IsModified() )
        {
            pDoc->SetModifiedFlag();
        }
    }
}

void CMainFrame::OnUpdateViewBanknames(CCmdUI *pCmdUI)
{
    pCmdUI->Enable( ( GetDoc() != NULL ) ? TRUE : FALSE );
}

CRealGNX4LibDoc * CMainFrame::GetDoc()
{
    CRealGNX4LibDoc *pDoc = NULL;
    CChildFrame *pChild = (CChildFrame*)MDIGetActive();
    if( pChild )
        pDoc = (CRealGNX4LibDoc *)pChild->GetActiveDocument(); // is any document open?

    return pDoc;
}

CChildFrame * CMainFrame::GetActiveChild()
{
    return (CChildFrame*)MDIGetActive();
}

void CMainFrame::OnUpdateMovePresetUp(CCmdUI *pCmdUI)
{
    BOOL bEnable = FALSE;
    CChildFrame *pChild = GetActiveChild();
    if( pChild )
        bEnable = pChild->CanMoveUp();
    pCmdUI->Enable( bEnable );
}

void CMainFrame::OnUpdateMovePresetDown(CCmdUI *pCmdUI)
{
    BOOL bEnable = FALSE;
    CChildFrame *pChild = GetActiveChild();
    if( pChild )
        bEnable = pChild->CanMoveDown();
    pCmdUI->Enable( bEnable );
}

void CMainFrame::OnMovePresetUp()
{
    CChildFrame *pChild = GetActiveChild();
    if( pChild )
        pChild->MovePresetUp();
}

void CMainFrame::OnMovePresetDown()
{
    CChildFrame *pChild = GetActiveChild();
    if( pChild )
        pChild->MovePresetDown();
}

void CMainFrame::OnLoadFromG4PFile()
{
    CChildFrame *pChild = GetActiveChild();
    if( pChild )
    {
        const char szFilter[] = "Preset Files (*.g4p)|*.g4p|All Files (*.*)|*.*||";
        CFileDialog dlg( TRUE, "g4p", NULL, OFN_FILEMUSTEXIST, szFilter, this );

        if( IDOK == dlg.DoModal() )
        {
            pChild->LoadG4PFile( dlg.GetPathName() );
        }
    }
}